package com.imessage.action.device.virtual;

import com.bean.DeviceClient;
import com.bean.DeviceVirtual;
import com.imessage.action.BaseAction;
import com.imessage.service.BaseService;
import com.imessage.type.BusinessTaskCmd;
import com.imessage.type.BusinessTaskState;
import com.imessage.type.DeviceClientState;
import com.imessage.type.DeviceVirtualState;
import com.imessage.vo.ClientVo;
import com.imessage.vo.VirtualVo;
import io.ebean.Expr;
import io.ebean.Expression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

public class VirtualBaseAction extends BaseAction {

    protected final String datagrid = "datagrid-deviceVirtual-filter";

    protected void setExpressionList(List<Expression> expressionList, VirtualVo virtualVo) {
        if (StringUtils.isNotBlank(virtualVo.getClientName())) {
            expressionList.add(Expr.contains("DeviceClient.name", virtualVo.getClientName()));
        }
        if (StringUtils.isNotBlank(virtualVo.getVirtualMac())) {
            expressionList.add(Expr.contains("virtualMac", virtualVo.getVirtualMac()));
        }
        if (StringUtils.isNotBlank(virtualVo.getVirtualName())) {
            expressionList.add(Expr.contains("virtualName", virtualVo.getVirtualName()));
        }
        if (StringUtils.isNotBlank(virtualVo.getTaskCmd())) {
            expressionList.add(Expr.eq("taskCmd", virtualVo.getTaskCmd()));
        }
        if (StringUtils.isNotBlank(virtualVo.getTaskState())) {
            expressionList.add(Expr.eq("taskState", virtualVo.getTaskState()));
        }
        if (StringUtils.isNotBlank(virtualVo.getState())) {
            expressionList.add(Expr.eq("state", virtualVo.getState()));
        }
        if (StringUtils.isNotBlank(virtualVo.getClientId())) {
            expressionList.add(Expr.eq("DeviceClient", virtualVo.getClientId()));
        }
    }


    protected void setVirtual(VirtualVo virtualVo, DeviceVirtual deviceVirtual) {
        copyProperties(virtualVo, deviceVirtual);
        if (StringUtils.isNotBlank(virtualVo.getClientId())) {
            deviceVirtual.setDeviceClient(baseService.findById(DeviceClient.class, Long.valueOf(virtualVo.getClientId())));
        }
        if (StringUtils.isNotBlank(virtualVo.getState())) {
            deviceVirtual.setState(DeviceVirtualState.fromValue(virtualVo.getState()));
        }
        if (StringUtils.isNotBlank(virtualVo.getTaskState())) {
            deviceVirtual.setTaskState(BusinessTaskState.fromValue(virtualVo.getTaskState()));
        }
        if (StringUtils.isNotBlank(virtualVo.getTaskCmd())) {
            deviceVirtual.setTaskCmd(BusinessTaskCmd.fromValue(virtualVo.getTaskCmd()));
        }
    }

    protected void setModelAndView(ModelAndView modelAndView, DeviceVirtual deviceVirtual, DeviceClient client) {
        modelAndView.addObject("deviceVirtualForm", deviceVirtual);
        modelAndView.addObject("deviceClientForm", client);
        modelAndView.addObject("statusList", DeviceVirtualState.values());
        modelAndView.addObject("clientList", baseService.findList(DeviceClient.class, Expr.eq("1", 1)));
        modelAndView.addObject("taskCmdList", BusinessTaskCmd.values());
        modelAndView.addObject("taskStateList", BusinessTaskState.values());
    }


}
